package com.example.controller;

import com.example.Util.GDUtil;
import com.example.VO.*;
import com.example.model.NetErrorCode;
import com.example.model.UserRequest;
import com.example.service.RequestService;
import com.example.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by ykbboom on 2017/3/5.
 */
@RestController
@EnableAutoConfiguration
@RequestMapping(value = "/easygo/request/")
public class RequestController extends BaseController {
    private Logger logger = Logger.getLogger(RequestController.class);
    @Autowired
    private RequestService requestService;
    @Autowired
    private UserService userService;
    /**
     * 发布请求
     */
    @RequestMapping(value = "publish", method = RequestMethod.POST)
    public BaseResultVO publishRequest(@RequestParam String picurl, @RequestParam String description,
                                       @RequestParam String place, @RequestParam String destination,
                                       @RequestParam String time,@RequestParam String userPhone,@RequestParam String latlongStart,
                                       @RequestParam String latlongDes) {
        logger.info("运行publishRequest()方法/" + "picUrl:" + picurl + "  " + "/" + "起点:" + place + "/"+"起始经纬度"+latlongStart);
        int userId = userService.getUserId(userPhone);
        logger.info("userId:"+userId);
        if (requestService.publishRequest(userPhone,new UserRequest(description, place, destination,time,picurl,userId,latlongStart,latlongDes))!=null){
            return new BaseResultVO(null, BaseResultVO.RESULT_CODE_OK, "发布成功");
        }else {
            return new BaseResultVO(new ErrorResult(NetErrorCode.SERVER_UNKNOWN_ERROR,"服务器未知异常"),BaseResultVO.RESULT_CODE_FAIL,null);
        }

    }
    /**
     * 检索周围请求
     * */
    @RequestMapping(value = "aroundSearch",method = RequestMethod.POST)
    public AroundSearchVO aroundSearchRequest(@RequestParam String userPhone,@RequestParam String center,@RequestParam String radius){
        AroundSearchVO aroundSearchVO = requestService.aroundSearch(userPhone,GDUtil.swapLatLong(center),radius);
        logger.info(aroundSearchVO.toString());
        return aroundSearchVO;
    }
    /**
     * 获取请求详情
     * */
    @RequestMapping(value = "getRequestDetail",method = RequestMethod.POST)
    public UserRequestDetailVO getRequestDetail(@RequestParam String gdIdStart){
        logger.info("运行getRequestDetail()方法，gdIdStart为"+gdIdStart);
        UserRequestDetailVO userRequestDetailVO = requestService.getRequestDetail(Integer.parseInt(gdIdStart));
        return userRequestDetailVO;
    }
    /**
     * 获取用户的全部请求
     * */
    @RequestMapping(value = "getUserAllRequest",method = RequestMethod.POST)
    public UserAllRequestVO getUserAllRequest(@RequestParam String userPhone,@RequestParam String count){
        logger.info("运行getUserAllRequest()方法");
        UserAllRequestVO userAllRequestVO = requestService.getUserAllRequest(userPhone, Integer.valueOf(count));
        return userAllRequestVO;
    }
    /**
     * 删除用户请求
     * */
    @RequestMapping(value = "deleteUserRequest",method = RequestMethod.POST)
    public BaseResultVO deleteUserRequest(@RequestParam String requestId){
        logger.info("运行deleteUserRequest()方法");
        BaseResultVO baseResultVO = requestService.deleteUserRequest(Integer.parseInt(requestId));
        return baseResultVO;
    }
    /**
     * 接受请求
     * */
    @RequestMapping(value = "acceptRequest",method = RequestMethod.POST)
    public BaseResultVO acceptRequest(@RequestParam String userPhone,@RequestParam String gdIdStart){
        BaseResultVO baseResultVO = requestService.acceptRequest(userPhone, Integer.parseInt(gdIdStart));
        return baseResultVO;
    }
    /**
     * 获取用户所有接单
     * */
    @RequestMapping(value = "getUserAllOrder",method = RequestMethod.POST)
    public UserAllOrderVO getUserAllOrder(@RequestParam String userPhone,@RequestParam String count){
        UserAllOrderVO userAllOrderVO = requestService.getUserAllOrder(userPhone, Integer.parseInt(count));
        return userAllOrderVO;
    }
    /**
     * 用户反馈投诉
     * */
    @RequestMapping(value = "userReport",method = RequestMethod.POST)
    public BaseResultVO userReport(@RequestParam String userPhone,@RequestParam String content){
        BaseResultVO baseResultVO = requestService.addUserReport(userPhone,content);
        return baseResultVO;
    }
    /**
     * 车主完成请求
     * */
    @RequestMapping(value = "driverCompleteOrder",method = RequestMethod.POST)
    public BaseResultVO driverCompleteOrder(@RequestParam String userPhone,@RequestParam String center
            ,@RequestParam String requestId){
        logger.info("车主提交完成订单数据，经纬度:"+center+"/请求Id+:"+requestId);
        BaseResultVO baseResultVO = requestService.driverCompleteOrder(userPhone,center, Integer.parseInt(requestId));
        return baseResultVO;
    }
    /**
     * 车主增加积分
     * */
    @RequestMapping(value = "addDriverPoints",method = RequestMethod.POST)
    public UserVO addDriverPoints(@RequestParam String requestId){
        UserVO userVO = requestService.addDriverPoints(Integer.parseInt(requestId));
        return userVO;
    }

    /**
     * 用户查看是否有请求被接受
     * */
    @RequestMapping(value = "getNotifyFlag",method = RequestMethod.POST)
    public BaseResultVO getNotifyFlag(@RequestParam String userPhone){
        logger.info("访问查看通知方法");
        BaseResultVO baseResultVO = requestService.getNotifyFlag(userPhone);
        logger.info(baseResultVO.toString());
        return baseResultVO;
    }
}